<template>
	<view class="pageBox">
		<!-- <view class="navbar-fixed">
			<view class="tabsBG">
				<u-tabs lineWidth="60rpx" lineHeight="4rpx" :list="dictList.volume_type" keyName="label"
					:itemStyle="tabsStyle" :activeStyle="tabsAStyle" lineColor="#0138D2" :current="tabIndex"
					@change="tabsChange"></u-tabs>
			</view>
		</view> -->
		<!-- 对于mescroll-body: 需设置:sticky="true", 此应避免在mescroll-body标签前面加其他非定位的元素, 否则下拉区域会被挤出, 无法会隐藏.-->
		<!-- 对于mescroll-uni: 则无需设置:sticky="true", 无其他限制和要求 -->
		<mescroll-body :sticky="true" ref="mescrollRef" :top="mescroll_top" @init="mescrollInit" @down="downCallback"
			:down="downOption" @up="upCallback" :up="upOption">
			<view class="couponContentBox">
				<couponItem v-for="(item,index) in dataList.rows" :key="index" :dataItem="item" nameKey="volumeName"
					@btnClick="goto('my/couponPage/couponInfo/couponInfo',{id:item.volumeId})" btnText="去使用"
					:rightBtn="item.exhibitionCode>0">
				</couponItem>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	// import searchRow from '@/pages/search/searchRow/searchRow.vue'
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js'
	import * as volumeUserInfoApi from '@/api/volumeUserInfo.js'
	import * as systemApi from '@/api/system.js'
	export default {
		mixins: [MescrollMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		// components: {
		// 	searchRow
		// },
		data() {
			return {
				mescroll_top: '88rpx',
				searchUrl: 'pages/search/afterSales_Search',
				tabsStyle: {
					height: '88rpx',
					boxSizing: 'border-box'
				},
				applyTypeList: ['全部退款', '换货', '已发货退货退款', '未发货退货退款 '],
				tabsAStyle: {
					color: '#0138D2'
				},
				tabs: [{
						name: '未使用',
						id: 0
					},
					{
						name: '已使用 ',
						id: 1
					},
					{
						name: '已过期',
						id: 2
					},

				],
				tabIndex: 0,
				tabIndexOld: 0,
				options: {},
				downOption: {
					auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					auto: false, // 不自动加载
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 8 // 每页数据的数量
					},
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					textNoMore: '--这里是底线--',
					empty: {
						tip: '暂无' // 提示
					}
				},
				dataList: {
					records: [],
					rows: [],
					pages: 0,
					size: 0,
					total: 0,
					current: 0
				}, //列表数据
				listObj: {
					// discountType: 0,
					volumeState: 0
				},
				canReset: false,
				dicts: {
					serve_state: {},
					volume_type: {}
				},
				dictList: {
					serve_state: [],
					volume_type: []
				},
				userInfo: {
					userId: ''
				}
			}
		},
		mounted() {
			const that = this
			// // #ifdef MP
			// uni.getSystemInfo({
			// 	success: function(res) {
			// 		that.mescroll_top = 88 + 88 + res.statusBarHeight * 2 + 'rpx'
			// 	}
			// })
			// // #endif
			if (this.$store.getters.platform === 'H5-WEIXIN') {
				that.mescroll_top = 0 + 'rpx'
			} else {
				uni.getSystemInfo({
					success: function(res) {
						that.mescroll_top = res.statusBarHeight * 2 + 'rpx'
					}
				})
			}
		},
		async onLoad(options) {
			this.options = options
			// this.getDictDataList('volume_type').then(res => {
			// 	this.listObj.discountType = res.keyValues[0].value
			// })
			// this.userInfo = this.$store.getters.my_userInfo || ''
			const page = {
				num: 1,
				size: 8
			}
			if (!this.userInfo.userId) {
				await this.$store.dispatch('userBaseInfo').then(res => {
					console.log(res);
					this.userInfo = res.user
					this.eliteInfo = res.eliteInfo
					this.$nextTick(() => {
						setTimeout(this.downCallback(), 300)
					})
				})
			} else {
				this.$nextTick(setTimeout(() => {
					this.upCallback(page)
				}, 300))
			}
		},
		onShow() {
			// that.canReset && that.getData()
			// that.canReset = true
		},
		methods: {
			getDictDataList(dictType) {
				return new Promise((resolve, reject) => {
					systemApi.dictDataList(dictType).then(({
						rows
					}) => {
						let objs = {}
						let keyValues = []
						rows.forEach((item) => {
							objs[item.dictValue] = item.dictLabel
							keyValues.push({
								label: item.dictLabel,
								text: item.dictLabel,
								value: item.dictValue
							})
						})
						this.dicts[dictType] = objs
						this.dictList[dictType] = keyValues
						resolve({
							objs,
							keyValues,
							rows
						})
					}).catch(reject)
				})
			},
			goto(url, data) {
				this.$navTo(`pages/${url}`, data)
			},
			//手动刷新
			getData() {
				let curMescroll = this.$refs.mescrollItem[0].mescroll
				curMescroll && curMescroll.resetUpScroll()
				curMescroll && curMescroll.scrollTo(0, 0)
			},
			tabsChange(e) {
				console.log(e)
				this.tabIndexOld = this.tabIndex
				this.tabIndex = e.index
				this.listObj.discountType = e.value
				this.downCallback()
			},
			/*下拉刷新的回调 */
			downCallback(page) {
				// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
				// loadSwiper();
				// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
				// this.$refs.mescrollRef.resetUpScroll()
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				//联网加载数据
				const that = this
				console.log(this.userInfo.userId);
				if (that.$store.getters.token) {
					this.getDataList({
						pageNum: page.num,
						pageSize: page.size,
						userId: this.userInfo.userId,
						...that.listObj
					}).then(list => {
						const curPageLen = list.rows.length
						const totalSize = list.total
						that.mescroll.endBySize(curPageLen, totalSize)
					}).catch(err => {
						that.mescroll.endErr()
					})
				} else {
					that.mescroll.endBySize(0, 0)
					return
				}
			},
			/**
			 * @param {Object} page 页码
			 */
			getDataList(pageObj) {
				const that = this
				return new Promise((resolve, reject) => {
					volumeUserInfoApi.volumeUserInfoList(pageObj).then((
						data
					) => {
						let dataList = JSON.parse(JSON.stringify(that.dataList))
						dataList.pageSize = pageObj.pageSize
						dataList.pageNum = pageObj.pageNum
						dataList.total = data.total
						dataList.pages = Math.ceil(data.total / dataList.pageSize)
						// 合并新数据
						const newList = data.rows
						// 如果是第一页需手动制空列表
						if (pageObj.pageNum == 1) dataList.rows = []
						// 合并新数据
						dataList.rows = dataList.rows.concat(newList)
						that.dataList = dataList
						resolve(that.dataList)
					}).catch(reject)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	$nav-height: 30px;

	.pageBox {
		.tabsBG {
			padding-left: 30rpx;
			background-color: #F4F7FF;
		}

		min-height: 100vh;
		box-sizing: border-box;
		background-color: #F4F7FF;

		.couponContentBox {
			padding: 30rpx;
		}

	}
</style>